package com.green.common.fusioncharts;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 折线图
 * @author yinyuqiao
 * @time 2013-1-7 上午11:25:14
 */
public class LineChartDataFormat {
	private Map<String, Object> map;
	private StringBuffer buffer;
	private double maxX;
	private double minX;
	private double maxY;
	private double minY;
						
	private String caption = null; // 图的名字
	private String yAxisName = null; // y轴名称 此处中文不能正确显示
	private String xAxisName = null; // x轴名称
	private String decimalPrecision = "0"; //小数点位数
	private String formatNumberScale = "0"; 
	private String decimals="0";
	
	private List<ChartXLabel> label;
	
	private String getLabel() {// chart的横坐标X轴的分隔线
		StringBuffer temp = new StringBuffer();
		temp.append(" <categories font='Arial' fontSize='11' fontColor='000000'>");
		for (ChartXLabel obj : label) {
			temp.append("<category name=\'");
			
			temp.append(obj.getName());
			temp.append("\' />");
		}
		temp.append("</categories>");
		return temp.toString();
	}
	public void setLabel(List<ChartXLabel> label) {
		this.label = label;
	}
	
	@SuppressWarnings("unused")
	private LineChartDataFormat(){
		
	}
	
	public LineChartDataFormat(String caption , String yAxisName ,String xAxisName ,String decimalPrecision,String formatNumberScale){
		this.caption = caption ;
		this.yAxisName = yAxisName ;
		this.xAxisName = xAxisName ;
		this.decimalPrecision = decimalPrecision;
		this.formatNumberScale = formatNumberScale;
	}
	/**
	 * 小区价格曲线--后台网站管理
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getGardenChart() {
		buffer = new StringBuffer();
//		setXY();
		try {
			buffer  
					.append("<graph unescapeLinks=\'0\' like=\'n-/qfang-web/garden' numberSuffix=\'元\' showNames='1' showValues='0' showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='20' alternateHGridAlpha='5' formatNumber=\'0\' caption=\'" 
							+ caption
							+ "\' decimalPrecision=\'"
							+ decimalPrecision
							+"\' forceDecimals=\'"
							+"2"
							+ "\' formatNumberScale=\'"
							+ formatNumberScale
							+ "\' yAxisName=\'"
							+ yAxisName   
							+ "\' xAxisName=\'"
							+ xAxisName
							+ "\' yAxisMaxValue=\'"
							+ maxY
							+ "\' yAxisMinValue=\'"
							+ minY
							+ "\' >");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		buffer.append(getGardenXLine(map));
		buffer.append("</graph>");
		return buffer.toString();
	}
	private String getGardenXLine(Map map) {
		StringBuffer temp = new StringBuffer();
		for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Object> e = (Map.Entry<String, Object>) it.next();
			String label = e.getValue().toString();
			String value = e.getKey();
			temp.append("<set value=\'");
			temp.append(e.getValue());
			temp.append("\' name=\'");
			temp.append(e.getKey()+"月");
			temp.append("\' />");
		}
		return temp.toString();
	}
	/**
	 * 小区价格曲线
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getChartStrs() {
		buffer = new StringBuffer();
		setXY();
		try {
			buffer  
					.append("<graph unescapeLinks=\'0\' like=\'n-/qfang-web/garden' numberSuffix=\'元\' showNames='1' showValues='0' showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='20' alternateHGridAlpha='5' caption=\'" 
							+ caption
							+ "\' decimalPrecision=\'"
							+ "0"
							+"\' forceDecimals=\'"
							+"0"
//							+"\'  yAxisValueDecimals=\'"
//							+ "0"
							+ "\'formatNumber=\'0" 
							+"\' formatNumberScale=\'"
							+"0"
							+ "\' yAxisName=\'"
							+ yAxisName   
							+ "\' xAxisName=\'"
							+ xAxisName
							+ "\' yAxisMaxValue=\'"
							+ maxY
							+ "\' yAxisMinValue=\'"
							+ minY
							+ "\' >");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		buffer.append(getXLine(map));
		buffer.append("</graph>");
		return buffer.toString();
	}
	private String getXLine(Map map) {
		StringBuffer temp = new StringBuffer();
		for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Object> e = (Map.Entry<String, Object>) it.next();
			Map<String,Object> label = (Map<String, Object>) e.getValue();
			String value = e.getKey();
			//Map.Entry<String, Object> e = (Map.Entry<String, Object>) it.next();
			Map<String,Object> obj = (Map<String, Object>) e.getValue();
			temp.append("<set value=\'");
			temp.append(obj.get("price"));
			temp.append("\' name=\'");
			temp.append(obj.get("month")+"月");
			temp.append("\' />");
		}
		return temp.toString();
	}
	/**
	 * 取柱状图
	 * @return
	 */
	public String getMultChartStr() {
		buffer = new StringBuffer();
		
		for(Iterator iter=(Iterator) map.entrySet().iterator();iter.hasNext();){
			Map.Entry<String, Object> e = (Map.Entry<String, Object>) iter.next();
			Map<String,Object> maps = (Map<String, Object>) e.getValue();
			if (maps != null) {
				List<String> lst=(List<String>) maps.get("data");
				for(int i=0;lst!=null && i<lst.size();i++){
					double data=Double.valueOf(lst.get(i));
					if (data > maxY) {
						maxY = data;
					}
					if(data < minY){
						minY = data;
					}
				}	
			}	
		}
		maxY =new BigDecimal(maxY).multiply(new BigDecimal("110")).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		minY=0;
		if(maxY==new Double("0").doubleValue()){
			maxY=1;
		}
		try {
			buffer  
					.append("<graph unescapeLinks=\'0\' like=\'n-/qfang-web/garden' numberSuffix=\'%\' showNames='1' showValues='0' showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='20' alternateHGridAlpha='5' formatNumber=\'0\' caption=\'" 
							+ caption
							+ "\' decimalPrecision=\'"
							+ decimalPrecision
							+"\' decimals=\'"
							+decimals
							+ "\' formatNumberScale=\'"
							+ formatNumberScale
							+ "\' yAxisName=\'"
							+ yAxisName   
							+ "\' xAxisName=\'"
							+ xAxisName
							+ "\' yAxisMaxValue=\'"
							+ maxY
							+ "\' yAxisMinValue=\'"
							+ minY
							+ "\' >");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		//buffer.append(getXLine(map));
		buffer.append(getLabel());
		for(Iterator iter=(Iterator) map.entrySet().iterator();iter.hasNext();){
			Map.Entry<String, Object> e = (Map.Entry<String, Object>) iter.next();
			Map<String,Object> maps = (Map<String, Object>) e.getValue();
			buffer.append(this.getDataSet(maps));
		}
		buffer.append("</graph>");
		return buffer.toString();
	}
	
	private String getDataSet(Map<String,Object> map){
		StringBuilder temp=new StringBuilder();
		temp.append("<dataset seriesname='"+map.get("name")+"' color='"+map.get("color")+"'> ");
		List<String> lst=(List<String>) map.get("data");
		for(int i=0;lst!=null && i<lst.size();i++){
			String str=lst.get(i);
			temp.append(" <set value='"+new BigDecimal(str).multiply(new BigDecimal("100")).setScale(2,BigDecimal.ROUND_UP)+"' />");
		}	
		temp.append("</dataset>");
		return temp.toString();
	}
	/**
	 * 设置X轴Y轴坐标
	 * 初始化绘图面板
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	public void setXY() { 
		maxY = 1.0;
		minY = 30000.0;
		if (map != null) {
			for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Map> e = (Map.Entry<String, Map>) it.next();
				double tempY = Double.parseDouble(e.getValue().get("price").toString());
				if (tempY > maxY) {
					maxY = tempY;
				}
				if(tempY < minY){
					minY = tempY;
				}
			}
		}	
		maxY = maxY*1.05 - maxY*1.05%500;
		minY = minY*0.95 - minY*0.95%500;
//		if(minY<10000){
//			minY=18760*1.05 - 18760*1.05%500
//		}else{
//			minY=new Double(minY).intValue()/10000*10000;
//		}
//		if(maxY<10000){
//			maxY=10000;
//		}else{
//			maxY=(new Double(maxY).intValue()/10000+1)*10000;
//		}
	}
	public void setMaxY(int maxY){
		this.maxY=maxY;
	}
	public void setMinY(int minY){
		this.minY=minY;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

}
